//
//  ITDeviceInfo.m
//  ITBE
//
//  Created by ITBE on 2019/11/5.
//  Copyright © 2019 ITBE. All rights reserved.
//

#import "ITDeviceInfo.h"

#import "ITNetWorkInfoManager.h"
#import "ITDeviceDataLibrery.h"
#import "ITBatteryInfoManager.h"

#import "ITAppStartManager.h"

#import "ITLocationManager.h"

#define DeviceTokenService2 @"com.client.videochat.deviceTokenService2"
#define DeviceTokenAccount2 @"com.client.videochat.deviceTokenAccount2"

@implementation ITDeviceInfo

static CGSize _screenSize;

+ (CGSize)screenSize{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _screenSize = UIScreen.mainScreen.bounds.size;
    });
    return _screenSize;
}

+ (NSString *)idfa{
    return [ITDeviceInfoManager.sharedManager getIDFA];
}

+ (NSString*)appVersion{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
}

+ (NSString *)deviceModelName{
    return [ITDeviceInfoManager.sharedManager getDeviceName];
}

+ (NSString *)deviceName{
    NSString *iPhoneName = [UIDevice currentDevice].name;
    return iPhoneName;
}

+(NSString*)getBundleID{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
}

+ (NSString*)getBundleDisplayName{
    return [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
}

+ (NSString *)systemVersion{
    NSString *systemVersion = [UIDevice currentDevice].systemVersion;
    return systemVersion;
}

+ (NSString *)language{
    NSString *language = [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode];
    return language;
}

+ (NSString *)country{
    NSString *country = [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode];
    return country;
}

+ (NSString *)region{
    NSString *region = [[NSLocale currentLocale] objectForKey:NSLocaleIdentifier];
    return region;
}

+ (int)timeZone{
    int timeZone = [[NSTimeZone localTimeZone] secondsFromGMT]/3600;
    return timeZone;
}

+ (NSString *)getDeviceToken2
{
    static NSString * _deviceToken = nil;
    if (_deviceToken == nil) {
        NSString *token = [YYKeychain getPasswordForService:DeviceTokenService2 account:DeviceTokenAccount2];
        if (token == nil) {
            token = [NSString stringWithUUID];
            [YYKeychain setPassword:token forService:DeviceTokenService2 account:DeviceTokenAccount2];
        }
        _deviceToken = token;
    }
    return _deviceToken;
}

+ (NSMutableDictionary *)monitorInfo{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:0];
    NSString *appid = @"12958364787519";
    NSString *key = [NSString stringWithFormat:@"Intube%@",appid];
    params[@"appid"] = appid;
    
    ITDeviceInfoManager *infoM = ITDeviceInfoManager.sharedManager;
    
    NSMutableDictionary *encryptDic = [NSMutableDictionary dictionary];
    encryptDic[@"id"] = [[ITUserManager shareManager] getDeviceToken] ?: @"";//唯一识别码 androidid
    encryptDic[@"did"] = [ITDeviceInfo idfa] ?: @"";//设备id android: build.id
    encryptDic[@"lid"] = [self getDeviceToken2] ?: @"";// 本地唯一ID  应用写入本地文件的UUID
    //用户ID  例：10000
    encryptDic[@"uid"] = ITUserManager.currentMember.memberId ?: @"";
    
    encryptDic[@"dd"] = [self deviceName] ?: @"";//显示器id
    encryptDic[@"dn"] = @"iPhone";//设备名
    encryptDic[@"dp"] = @"iPhone";//产品名
    encryptDic[@"dob"] = @"";//主板
    encryptDic[@"dma"] = @"Apple";//厂商
    encryptDic[@"dbr"] = @"iPhone";//品牌
    encryptDic[@"dmo"] = [self deviceModelName] ?: @"";//型号
    encryptDic[@"dbot"] = [self systemVersion] ?: @"";//系统版本号
    encryptDic[@"dhar"] = @"";//硬件名__
    encryptDic[@"ds"] = @"";//设备序列号
    encryptDic[@"dtp"] = @"";//builder类型
    encryptDic[@"dtg"] = @"";//builder标签
    encryptDic[@"dus"] = [self deviceModelName] ?: @"";//用户名
    encryptDic[@"dfp"] = @"";//设备指纹 idfa
    
    encryptDic[@"llan"] = [ITDeviceInfo language] ?: @"";//语言
    encryptDic[@"lre"] = [self region] ?: @"";//区域
    encryptDic[@"lc"] = [self country] ?: @"";//国家
    encryptDic[@"ltz"] = @([self timeZone]).stringValue;//时区
    NSDate *d = NSDate.date;
    encryptDic[@"ld"] = d.description ?: @"";//日期
    NSString *timestamp = [NSString stringWithFormat:@"%.0f", d.timeIntervalSince1970];
    encryptDic[@"lu"] = timestamp;//时间戳
    
    NSDate *systemUptime = [infoM getSystemUptime];
    encryptDic[@"lr"] = systemUptime.description ?: @"";//上次设备重启时间
    encryptDic[@"lbt"] = @"";//系统最后升级时间 ro.build.time
    
    

    //encryptDic[@"cn"] = [ITDeviceDataLibrery.sharedLibrery getCPUProcessor];//cpu型号
    encryptDic[@"cn"] = [[ITDeviceInfoManager sharedManager] cpu] ?: @"";//cpu型号
    CGFloat cpuUsage = [[ITDeviceInfoManager sharedManager] getCPUUsage];
    encryptDic[@"cr"] = @(cpuUsage).stringValue;//cpu使用率
    encryptDic[@"car"] = @"";//APP占用CPU的使用率
    encryptDic[@"cp"] = @"apple";//CPU平台
    encryptDic[@"cm"] = @"";//CPU页内存
    NSUInteger cpuCount = [[ITDeviceInfoManager sharedManager] getCPUCount];
    encryptDic[@"cc"] = @(cpuCount).stringValue;//CPU核数
    encryptDic[@"csr"] = [ITDeviceInfoManager.sharedManager getPerCPUUsageString] ?: @"";//单CPU使用率 例："0.26,0.03,0.19,0.03,0.15,0.02,0.11,0.02"
    
    int64_t totalMemory = [[ITDeviceInfoManager sharedManager] getTotalMemory];
    encryptDic[@"mt"] = @(totalMemory).stringValue;//总内存大小
    int64_t freeMemory = [infoM getFreeMemory];
    encryptDic[@"mf"] = @(freeMemory).stringValue;//可用内存大小 mem.free
    int64_t usedMemory = [infoM getFreeDiskSpace];
    encryptDic[@"mu"] = @(usedMemory).stringValue;//已用内存大小 mem.used
    NSString *applicationSize = [infoM getApplicationSize];
    encryptDic[@"map"] = applicationSize ?: @"";//APP分配内存大小
    encryptDic[@"mapu"] = @"";//APP已用内存大小
    encryptDic[@"mapf"] = @"";//APP空闲内存大小
    int64_t activeMemory = [infoM getActiveMemory];
    encryptDic[@"mac"] = @(activeMemory).stringValue;//活跃内存大小  mem.active
    encryptDic[@"mb"] = @"";//缓冲区内存大小  mem.buffers
    encryptDic[@"mca"] = @"";//缓存内存大小  mem.cached
    encryptDic[@"msw"] = @"";//交换空间内存大小 mem.swapcached
    encryptDic[@"mswt"] = @"";//交换空间总大小 mem.swap.total
    encryptDic[@"mswf"] = @"";//交换空间空闲大小 mem.swap.free
    
    int64_t totalDisk = [infoM getTotalDiskSpace];
    encryptDic[@"st"] = @(totalDisk).stringValue;//磁盘总空间
    int64_t usedDisk = [infoM getUsedDiskSpace];
    encryptDic[@"su"] = @(usedDisk).stringValue;//磁盘已使用空间
    int64_t freeDisk = [infoM getFreeDiskSpace];
    encryptDic[@"sf"] = @(freeDisk).stringValue;//磁盘空闲空间
    encryptDic[@"sd"] = @"N";//是否有SD卡 Y|N
    
    encryptDic[@"diw"] = @(SCREEN_WIDTH).stringValue;//屏幕宽
    encryptDic[@"dih"] = @(SCREEN_HEIGHT).stringValue;//屏幕高
    encryptDic[@"dir"] = [NSString stringWithFormat:@"%fX%f",SCREEN_WIDTH, SCREEN_HEIGHT];//分辨率   例：1280x960
    encryptDic[@"dil"] = @"";//屏幕亮度   例：100
    encryptDic[@"dif"] = @"";//字体大小   例： 5
    encryptDic[@"dse"] = infoM.getMotionInfo ?: @"";//传感器坐标  例：x,y,z
    
    encryptDic[@"no"] = [ITLocationManager getCarrierName];//运营商编码
    encryptDic[@"non"] = ITNetWorkInfoManager.sharedManager.hasNetwork ? @"Y" : @"N";//是否联网   例: Y|N
    encryptDic[@"nt"] = ITNetWorkInfoManager.sharedManager.networkName ?: @"";//网络类型   例： wifi|3G|4G
    encryptDic[@"nwid"] = [ITLocationManager wifiSSID];//Wifi SSID
    NSString *wifiIP = [[ITNetWorkInfoManager sharedManager] getIpAddressWIFI];
    encryptDic[@"nwip"] = wifiIP ?: @"";//Wifi IP
    encryptDic[@"nwm"] = @"";//WIFI MAC地址
    encryptDic[@"nbon"] = @"";//TTNetWorkInfoManager.sharedManager.blueOn ? @"Y" : @"N";//是否打开蓝牙  例：Y|N
    encryptDic[@"nbn"] = @"";//蓝牙名称      例： 张三
    encryptDic[@"nv"] = ITLocationManager.isVPNConnected ? @"Y" : @"N";//是否使用VPN   例：Y|N
    encryptDic[@"np"] = ITLocationManager.proxyInfo.length>0 ? @"Y" : @"N";//是否使用代理   例：Y|N
    
    [[ITBatteryInfoManager sharedManager] startBatteryMonitoring];
    
    encryptDic[@"pr"] = [ITBatteryInfoManager sharedManager].charging ? @"Y" : @"N";//电池充电状态  例：Y|N
    encryptDic[@"pf"] = @([ITBatteryInfoManager sharedManager].levelPercent).stringValue;//电池可用电量  例： 88
    
    encryptDic[@"ap"] = ITTrackManager.shareManager.pageName ?: @"";//当前所在的APP页面   例：task/withdraw
    encryptDic[@"apt"] = @(ITTrackManager.shareManager.pageShowedTime).stringValue;//当前所在页面停留时长（秒）  例： 50
    encryptDic[@"at"] = @(ITTrackManager.shareManager.appUsedTime).stringValue;//APP打开时长 （秒）  例：300
    
    encryptDic[@"imei"] = @"";//imei
    
    encryptDic[@"timestamp"] = timestamp;//时间戳
    //encryptDic[@"uuid"] = NSString.stringWithUUID ?: @"";//uuid串每次不一样
    //params[@"data"] = [ITStrategyData encrypt:[encryptDic jsonStringEncoded] withKey:key];
    return params;
}

@end
